#function for calculating proportions
prop <- function(x){
  prop.table(table(x))
}

#load non stacked data
load("dat71524.RData")

#Figure 1
library(scales)
mat <- matrix(c(as.numeric(prop(dat[dat$ethnicity==2,6])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,6])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,7])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,7])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,3])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,3])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,2])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,2])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,4])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,4])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,5])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,5])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,1])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,1])[4]),
                as.numeric(prop(dat[dat$ethnicity==2,8])[4]),
                as.numeric(prop(dat[dat$ethnicity==1,8])[4])),nrow=2,
              dimnames=list(c("Black Respondent","White Respondents"),
                            c("Race","Ethnicity","Gender","Education","Religion","Ideology","Geography","Sexual\nOrientation")))
barplot(mat,beside=T,legend=T,ylab="Proportion: 'Very important'",
        ylim=c(0,0.5307787),yaxs="i",xlim=c(0,25),
        main="Importance of Different Types of Diversity\non the Supreme Court",
        col=c(alpha("black",.3),alpha("gray",.4)))
arrows(x0=c(1.5,2.5,4.5,5.5,7.5,8.5,10.5,11.5,13.5,14.5,16.5,17.5,19.5,20.5,22.5,23.5),
       y0=c(0.4478690,0.2457074,0.4018659,0.2516064,0.3807553,0.2560358,0.3474916,0.2207274,0.2747065,0.1929966,0.2592543,0.2236583,0.2541158,0.1640492,0.2370335,0.1799373),
       x1=c(1.5,2.5,4.5,5.5,7.5,8.5,10.5,11.5,13.5,14.5,16.5,17.5,19.5,20.5,22.5,23.5),
       y1=c(0.5307787,0.3147827,0.4842551,0.3211348,0.4626610,0.3258937,0.4283091,0.2876953,0.3516281,0.2572331,0.3350518,0.2908899,0.3295141,0.2249248,0.3110092,0.2427274),
       angle=90,length=.025,code=3)

#Do you believe the Supreme Court is more responsive to the issues you care about when some of the justices share your identity?
prop(dat$rep2[dat$ethnicity==1]) #white respondents
prop(dat$rep2[dat$ethnicity==2]) #black respondents

#Figure A7
plot(1:8,
     c(as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,6])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,7])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,3])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,2])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,4])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,5])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,1])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,8])[4])),
     pch=19,ylim=c(0.1640492,0.5307787),xlim=c(.5,9),
     xaxt="n",xlab="",ylab="",main="Importance of Diversity on the Supreme Court")
title(ylab="Proportion: 'Very important'",line=2)
points(1:8,
       c(as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,6])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,7])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,3])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,2])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,4])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,5])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,1])[4]),
         as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,8])[4])),
       col="gray",pch=19)
text(1:8,
     c(as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,6])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,7])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,3])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,2])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,4])[4]),
       as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,5])[4]),
       as.numeric(prop(dat[dat$gender==2&dat$ethnicity%in%1:2,1])[4]),
       as.numeric(prop(dat[dat$gender==1&dat$ethnicity%in%1:2,8])[4])),
     labels=c("Racial Diversity",
              "Ethnic Diversity",
              "Gender Diversity",
              "Educational Diversity",
              "Religious Diversity",
              "Ideological Diversity",
              "Geographical Diversity",
              "Sexual Orientation Diversity"),pos=c(1,3,3,3,3,3,3,3))
legend("topright",legend=c("Female Respondents","Male Respondents"),pch=19,col=c("black","gray"))

#load stacked (conjoint) data and subset to relevant data
load("dat_long71524.RData")
master <- master[master$Race%in%c("Black","White"),]
master <- master[master$ethnicity%in%1:2,]

#Descriptive Statistics for DV items
library(ltm); library(psych)
cronbach.alpha(na.omit(master[,c("Support","Trust","Quality")]))
cronbach.alpha(na.omit(master[,c("SCOTUS_App","SCOTUS_leg","SCOTUS_agr")]))
factanal(na.omit(master[,c("Support","Trust","Quality")]),1)
factanal(na.omit(master[,c("SCOTUS_App","SCOTUS_leg","SCOTUS_agr")]),1)
pairs.panels(na.omit(master[,c("Trust","Quality","Support")]))
pairs.panels(na.omit(master[,c("SCOTUS_App","SCOTUS_agr","SCOTUS_leg")]))

#Figure A4 (raw experimental results)
t.test(master$JudgeScores[master$ethnicity==2&master$Race=="White"])
t.test(master$JudgeScores[master$ethnicity==2&master$Race=="Black"])
t.test(master$JudgeScores[master$ethnicity==1&master$Race=="White"])
t.test(master$JudgeScores[master$ethnicity==1&master$Race=="Black"])
t.test(master$CourtScores[master$ethnicity==2&master$Race=="White"])
t.test(master$CourtScores[master$ethnicity==2&master$Race=="Black"])
t.test(master$CourtScores[master$ethnicity==1&master$Race=="White"])
t.test(master$CourtScores[master$ethnicity==1&master$Race=="Black"])
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1),
     c(0.5484254,0.6243714,0.5864087,0.5653455,0.5659846,0.6312298,0.5897817,0.5769914),
     ylim=c(0.5262688,0.6512839),xlim=c(.5,4.5),
     xlab="",xaxt="n",ylab="",main="Basic Experimental Results",
     pch=21,bg="white",lwd=2,col="white")
segments(x0=c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1),
         y0=c(0.5262688,0.6008013,0.5661174,0.5446785,0.5478016,0.6111757,0.5722205,0.5586378),
         x1=c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1),
         y1=c(0.5705821,0.6479415,0.6067001,0.5860125,0.5841676,0.6512839,0.6073430,0.5953450))
points(c(.9,1.1,1.9,2.1,2.9,3.1,3.9,4.1),
       c(0.5484254,0.6243714,0.5864087,0.5653455,0.5659846,0.6312298,0.5897817,0.5769914),
       bg="white",pch=c(21,24),lwd=2:1)
legend("topright",legend=c("Black Judge","White judge"),
       pch=c(24,21),
       title="Respondent Race",bty="n")
axis(1,at=1:4,tick=F,
     labels=c("Judge Support\n(Black Resp.)","Judge Support\n(White Resp.)",
              "Court Support\n(Black Resp.)","Court Support\n(White Resp.)"))

#SEM (see Table 3)
library(lavaan)
test <- master
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)

#Causal Mediation Analysis (Figure 2)
library(mediation)
test <- na.omit(master[,c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#SEM and Mediation Analysis (Democratic Respondents) - See Table A6 & Figure 3
test <- master
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
'
fit1 <- sem(model,data=test[test$political_party3=="1",],cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$political_party3=="1",c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,7.1,4.1,.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical\nNominee on Court Support",
     xlim=c(-0.056539827,0.13754999),
     main="Democratic\nRespondents")
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white")
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#SEM and Mediation Analysis (Non-Democratic Respondents) - See Table A6 & Figure 3
test <- master
fit1 <- sem(model,data=test[test$political_party3!="1",],cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$political_party3!="1",c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical\nNominee on Court Support",
     xlim=c(-0.056539827,0.13754999),
     main="Non-Democratic\nRespondents")
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))

#Sensitivity Analyses (See Appendix E.2)
test <- master
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
'
test <- na.omit(master[,c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth,data = test[test$black==1,])
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth,data = test[test$black==1,])
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",cluster=test$rid[test$black==1],sims=10000)
sens.out.black <- medsens(med.black,sims=10000,rho.by=.01)
plot(sens.out.black)
plot(sens.out.black, sens.par = "R2", r.type = "total", sign.prod = "positive")

#SEM and Mediation Analysis (Co-Partisan Nominee) - See Table A7 & Figure 4
test <- master[master$political_party3%in%1:2,]
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
'
fit1 <- sem(model,data=test[test$shared_p==1,],cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(test[test$shared_p==1,c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,7.1,4.1,.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical\nNominee on Court Support",
     xlim=c(-0.056539827,0.13754999),
     main="Co-Partisan\nNominees")
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white")
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#SEM and Mediation Analysis (Out-Partisan Nominee) - See Table A7 & Figure 4
test <- master[master$political_party3%in%1:2,]
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
'
fit1 <- sem(model,data=test[test$shared_p!=1,],cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(test[test$shared_p!=1,c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical\nNominee on Court Support",
     xlim=c(-0.056539827,0.13754999),
     main="Out-Partisan\nNominees")
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.1,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))

#SEM and Causal Mediation Analysis with racial resentment measure excluded (see pages xi and xii of appendix)
test <- master
model <- '
JudgeScores ~ Black+black+black:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+Black+black+black:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[,c("JudgeScores","CourtScores","Black","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ Black+black+black:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+Black+black+black:Black+age+gender+hhi+rep+ind+oth, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#SEM and Causal Mediation Analysis using Black Feeling Thermometer instead of racial resentment (see pages xiii and xiv of appendix)
test <- master
model <- '
JudgeScores ~ thermometers_3+Black+black+thermometers_3:Black+black:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+thermometers_3+Black+black+thermometers_3:Black+black:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[,c("JudgeScores","CourtScores","thermometers_3","Black","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ thermometers_3+Black+black+thermometers_3:Black+black:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+thermometers_3+Black+black+thermometers_3:Black+black:Black+age+gender+hhi+rep+ind+oth, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#Conjoint Analysis
library(cregg)
test <- master
test$Sex <- factor(test$Sex)
test$same_sex[test$Sex=="Female"&test$gender==2] <- "same sex"
test$same_sex[test$Sex=="Male"&test$gender==1] <- "same sex"
test$same_sex[test$Sex=="Female"&test$gender==1|test$Sex=="Male"&test$gender==2] <- "different sex"
test$same_sex <- factor(test$same_sex)
test$same_race_white[test$Race=="White"&test$ethnicity==1] <- "same race white"
test$same_race_white[test$Race=="Black"&test$ethnicity==2] <- "same race black"
test$same_race_white[test$Race=="White"&test$ethnicity==2] <- "different race black"
test$same_race_white[test$Race=="Black"&test$ethnicity==1] <- "different race white"
test$same_race_white <- factor(test$same_race_white)
#Figure A8
cj(na.omit(test[,c("CourtScores","Sex","rid")]),CourtScores~Sex,id=~rid,estimate="mm")
cj(na.omit(test[,c("JudgeScores","Sex","rid")]),JudgeScores~Sex,id=~rid,estimate="mm")
#Figure A9
cj(na.omit(test[,c("CourtScores","Sex","same_sex","rid","gender")]),CourtScores~same_sex,id=~rid,estimate="mm",by=~gender)
#Figure A10
cj(na.omit(test[,c("JudgeScores","Sex","same_sex","rid","gender")]),JudgeScores~same_sex,id=~rid,estimate="mm",by=~gender)
#Figure A11
cj(na.omit(test[,c("SCOTUS_App","same_sex","rid","same_race_white")]),SCOTUS_App~same_sex,id=~rid,estimate="mm",by=~same_race_white)
cj(na.omit(test[,c("SCOTUS_agr","same_sex","rid","same_race_white")]),SCOTUS_agr~same_sex,id=~rid,estimate="mm",by=~same_race_white)
cj(na.omit(test[,c("SCOTUS_leg","same_sex","rid","same_race_white")]),SCOTUS_leg~same_sex,id=~rid,estimate="mm",by=~same_race_white)
#Figure A12
cj(na.omit(test[,c("Support","same_sex","rid","same_race_white")]),Support~same_sex,id=~rid,estimate="mm",by=~same_race_white)
cj(na.omit(test[,c("Trust","same_sex","rid","same_race_white")]),Trust~same_sex,id=~rid,estimate="mm",by=~same_race_white)
cj(na.omit(test[,c("Quality","same_sex","rid","same_race_white")]),Quality~same_sex,id=~rid,estimate="mm",by=~same_race_white)

#Conjoint diagnostics (see appendix B.7)
mod1 <- lm(JudgeScores~Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party,data=test)
mod2 <- lm(JudgeScores~(Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party)*factor(profile),data=test)
anova(mod1,mod2)
mod1 <- lm(CourtScores~Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party,data=test)
mod2 <- lm(CourtScores~(Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party)*factor(profile),data=test)
anova(mod1,mod2)
summary(lm(black~Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party,test))
mod1 <- lm(JudgeScores~Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party,data=test)
mod2 <- lm(JudgeScores~
             Sex*factor(Sex_order)+
             Age*factor(Age_order)+
             Race*factor(Race_order)+
             Religion*factor(Religion_order)+
             Ideology*factor(Ideology_order)+
             Philosophy*factor(Philosophy_order)+
             Education*factor(Education_order)+
             Party*factor(Party_order),data=test)
anova(mod1,mod2)
mod1 <- lm(CourtScores~Sex+Age+Race+Religion+Ideology+Philosophy+Education+Party,data=test)
mod2 <- lm(CourtScores~
             Sex*factor(Sex_order)+
             Age*factor(Age_order)+
             Race*factor(Race_order)+
             Religion*factor(Religion_order)+
             Ideology*factor(Ideology_order)+
             Philosophy*factor(Philosophy_order)+
             Education*factor(Education_order)+
             Party*factor(Party_order),data=test)
anova(mod1,mod2)

#Additional Analyses (see Appendix E.3)
test <- master[master$ethnicity==1,]
model <- '
JudgeScores ~ racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$ethnicity==1,c("JudgeScores","CourtScores","racism2","Black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth, data = test)
plot(mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",cluster=test$rid,sims=10000))
test <- master[master$ethnicity==2,]
model <- '
JudgeScores ~ racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$ethnicity==2,c("JudgeScores","CourtScores","racism2","Black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+racism2:Black+age+gender+hhi+rep+ind+oth, data = test)
plot(mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",cluster=test$rid,sims=10000))
test <- master[master$political_party3==2,]
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$political_party3==2,c("JudgeScores","CourtScores","racism2","black","Black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)
test <- master[master$political_party3==3,]
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[master$political_party3==3,c("JudgeScores","CourtScores","racism2","black","Black","age","gender","hhi","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+racism2:Black+black:Black+age+gender+hhi,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+racism2:Black+black:Black+age+gender+hhi, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)
load("dat_long71524.RData")
test <- master[master$ethnicity%in%1:2,]
model <- '
JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[,c("JudgeScores","CourtScores","racism2","Black","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+black+racism2:Black+black:Black+age+gender+hhi+rep+ind+oth, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",lwd=2,ylim=c(.5,3.5),
     yaxt="n",ylab="",xlab="Mediated Effect of Black Hypothetical Nominee\non Court Support",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1-.15,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1-.15,col="gray")
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1-.15,pch=21,bg="white",col="gray",lwd=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.15,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.15)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.15,pch=24,bg="white")
legend("topright",pch=c(24,21),legend=c("Black","White"),title="Respondent Race",bty="n",
       col=c("black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)
model <- '
JudgeScores ~ racism2+Black+Asian+Hispa+black+racism2:Black+black:Black+black:Asian+black:Hispa+age+gender+hhi+rep+ind+oth
CourtScores ~ JudgeScores+racism2+Black+Asian+Hispa+Asian+Hispa+black+racism2:Black+black:Black+black:Asian+black:Hispa+age+gender+hhi+rep+ind+oth
'
fit1 <- sem(model,data=test,cluster="rid")
summary(fit1)
fitMeasures(fit1)
test <- na.omit(master[,c("JudgeScores","CourtScores","racism2","Black","Asian","Hispa","black","age","gender","hhi","rep","ind","oth","rid")])
med.fit <- lm(JudgeScores ~ racism2+Black+Asian+Hispa+black+racism2:Black+black:Black+black:Asian+black:Hispa+age+gender+hhi+rep+ind+oth,data = test)
out.fit <- lm(CourtScores ~ JudgeScores+racism2+Black+Asian+Hispa+black+racism2:Black+black:Black+black:Asian+black:Hispa+age+gender+hhi+rep+ind+oth, data = test)
med.white <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.black <- mediate(med.fit,out.fit,treat="Black",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
med.asian1 <- mediate(med.fit,out.fit,treat="Asian",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.asian2 <- mediate(med.fit,out.fit,treat="Asian",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
med.hispa1 <- mediate(med.fit,out.fit,treat="Hispa",mediator="JudgeScores",covariates=list(black=0),cluster=test$rid,sims=10000)
med.hispa2 <- mediate(med.fit,out.fit,treat="Hispa",mediator="JudgeScores",covariates=list(black=1),cluster=test$rid,sims=10000)
par(mar=c(5.1,8.1,4.1,2.1))
plot(c(as.numeric(summary(med.black)[1]),
       as.numeric(summary(med.black)[9]),
       as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white",lwd=2,ylim=c(.5,3.5),main="Black Respondents",
     yaxt="n",ylab="",xlab="Mediated Effect of Judge Race on Court Support\n(relative to a white judge)",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.black)[[3]][1]),
              as.numeric(summary(med.black)[[11]][1]),
              as.numeric(summary(med.black)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.black)[[3]][2]),
              as.numeric(summary(med.black)[[11]][2]),
              as.numeric(summary(med.black)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.black)[1]),
         as.numeric(summary(med.black)[9]),
         as.numeric(summary(med.black)[25])),3:1+.1,pch=24,bg="white",lwd=2)
segments(x0=c(as.numeric(summary(med.asian2)[[3]][1]),
              as.numeric(summary(med.asian2)[[11]][1]),
              as.numeric(summary(med.asian2)[[26]][1])),
         y0=3:1,
         x1=c(as.numeric(summary(med.asian2)[[3]][2]),
              as.numeric(summary(med.asian2)[[11]][2]),
              as.numeric(summary(med.asian2)[[26]][2])),
         y1=3:1,col="gray")
points(c(as.numeric(summary(med.asian2)[1]),
         as.numeric(summary(med.asian2)[9]),
         as.numeric(summary(med.asian2)[25])),3:1,pch=22,bg="white",col="gray")
segments(x0=c(as.numeric(summary(med.hispa2)[[3]][1]),
              as.numeric(summary(med.hispa2)[[11]][1]),
              as.numeric(summary(med.hispa2)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.hispa2)[[3]][2]),
              as.numeric(summary(med.hispa2)[[11]][2]),
              as.numeric(summary(med.hispa2)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.hispa2)[1]),
         as.numeric(summary(med.hispa2)[9]),
         as.numeric(summary(med.hispa2)[25])),3:1-.1,pch=21,bg="white",col="gray")
legend("topright",pch=c(24,22,21),legend=c("Black","Asian","Hispanic"),title="Judge Race",bty="n",
       col=c("black","black","black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)
plot(c(as.numeric(summary(med.white)[1]),
       as.numeric(summary(med.white)[9]),
       as.numeric(summary(med.white)[25])),3:1+.1,pch=24,bg="white",lwd=2,ylim=c(.5,3.5),main="White Respondents",
     yaxt="n",ylab="",xlab="Mediated Effect of Judge Race on Court Support\n(relative to a white judge)",
     xlim=c(-0.056539827,0.13754999))
abline(v=0,lty=2)
segments(x0=c(as.numeric(summary(med.white)[[3]][1]),
              as.numeric(summary(med.white)[[11]][1]),
              as.numeric(summary(med.white)[[26]][1])),
         y0=3:1+.1,
         x1=c(as.numeric(summary(med.white)[[3]][2]),
              as.numeric(summary(med.white)[[11]][2]),
              as.numeric(summary(med.white)[[26]][2])),
         y1=3:1+.1)
points(c(as.numeric(summary(med.white)[1]),
         as.numeric(summary(med.white)[9]),
         as.numeric(summary(med.white)[25])),3:1+.1,pch=24,bg="white",lwd=2)
segments(x0=c(as.numeric(summary(med.asian1)[[3]][1]),
              as.numeric(summary(med.asian1)[[11]][1]),
              as.numeric(summary(med.asian1)[[26]][1])),
         y0=3:1,
         x1=c(as.numeric(summary(med.asian1)[[3]][2]),
              as.numeric(summary(med.asian1)[[11]][2]),
              as.numeric(summary(med.asian1)[[26]][2])),
         y1=3:1,col="gray")
points(c(as.numeric(summary(med.asian1)[1]),
         as.numeric(summary(med.asian1)[9]),
         as.numeric(summary(med.asian1)[25])),3:1,pch=22,bg="white",col="gray")
segments(x0=c(as.numeric(summary(med.hispa1)[[3]][1]),
              as.numeric(summary(med.hispa1)[[11]][1]),
              as.numeric(summary(med.hispa1)[[26]][1])),
         y0=3:1-.1,
         x1=c(as.numeric(summary(med.hispa1)[[3]][2]),
              as.numeric(summary(med.hispa1)[[11]][2]),
              as.numeric(summary(med.hispa1)[[26]][2])),
         y1=3:1-.1,col="gray")
points(c(as.numeric(summary(med.hispa1)[1]),
         as.numeric(summary(med.hispa1)[9]),
         as.numeric(summary(med.hispa1)[25])),3:1-.1,pch=21,bg="white",col="gray")
legend("topright",pch=c(24,22,21),legend=c("Black","Asian","Hispanic"),title="Judge Race",bty="n",
       col=c("black","black","black","gray"))
axis(2,at=3:1,c("Average Causal\nMediation Effect","Average\nDirect Effect","Total\nEffect"),
     las=2,tick=F)

#external validity test
load("external_validity_data.RData")
t.test(dat5$NOMINEE_d[dat5$ethnicity==2])
t.test(dat5$NOMINEE_d[dat5$ethnicity==1])
t.test(dat5$SCOTUS_d[dat5$ethnicity==2])
t.test(dat5$SCOTUS_d[dat5$ethnicity==1])
